package com.ay272.sshupdate.web;

import com.ay272.sshupdate.entity.User;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;


import org.springframework.ui.ModelMap;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by java on 2016/5/24.
 */
@Controller
@RequestMapping("home")
public class HomeController {
    @RequestMapping("/index")
    public String index(){
        System.out.println("访问网址：http://localhost:8080/sshupdate2/home/index.action");
        System.out.println("响应成功");
        return "home";
    }
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(User user, RedirectAttributesModelMap model){
        System.out.println("信息");
        System.out.println(user);
        model.addFlashAttribute("u",user);
        return "redirect:show.action";
    }
    @RequestMapping("/show")
    public String show(){
        return "show";
    }
    @RequestMapping("/ajaxSave")
    @ResponseBody
    public User save(User user){
        System.out.println(user);
        return user;
    }
    @RequestMapping("/listFile")
    public  String listFile(ModelMap model){
        File file = new File("d:/tsms");
        File[] files = file.listFiles();
        List<String>fileNames = new ArrayList<>();
        for (File f:files){
           fileNames.add(f.getName());
        }
        model.addAttribute("fileNames",fileNames);
        return "listFile";
    }
    /* 传统下载做法*/
   /* @RequestMapping("/download")
    public void download(String fileName,HttpServletResponse response) throws Exception{
        String name  = new String(fileName.getBytes("iso-8859-1"),"utf-8");
        response.setContentType("application/octet-stream");
        response.addHeader("Content-Disposition","attachment;filename="+fileName);
        FileInputStream in = new FileInputStream("d:/tsms/"+name);
        byte[] buffer = new byte[1024*1024];
        OutputStream out = response.getOutputStream();
        while (true){
          int n = in.read(buffer);
            if (n<(1024*1024)){
                out.write(buffer,0,n);
                out.flush();
                break;
            }
            out.write(buffer);
            out.flush();
        }
        in.close();
        out.close();
    }*/
    /*优化下载做法   解耦和做法*/
    @RequestMapping("/download")
    public ResponseEntity<FileSystemResource>a(String fileName) throws Exception{
        MultiValueMap<String,String>headers = new LinkedMultiValueMap<>();
        headers.add("Content-Disposition","attachment;filename="+fileName);
        return new ResponseEntity<FileSystemResource>(new FileSystemResource("d:/tsms/"+new String(fileName.getBytes("iso-8859-1"),"utf-8")),headers, HttpStatus.OK);
    }
    @RequestMapping(value = "fileUpload",method = RequestMethod.GET)
    public String uploadFile(){
        return "upload";
    }
    @RequestMapping(value = "fileUpload",method = RequestMethod.POST)
    public String upload(MultipartFile uploadFile)throws Exception{
        uploadFile.transferTo(new File("d:/tsms/"+uploadFile.getOriginalFilename()));
        return "redirect:listFile.action";
    }
}
